# ======================================================================
#
# Copyright (C) ADN
# ADN::CONF::Read Read.pm
# Description:
#  - Lecture du fichier de configuration de la sonde
#
# ======================================================================

package ADN::CONF::Read;

use strict;
use ADN::XML::OBJtoXML;
use ADN::XML::XMLtoOBJ;
use File::Path;
use vars qw($AUTOLOAD);

# #######################
# Création de l'objet self + init. variables
# #######################
sub new
{
	# Construction de l'objet $self
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
	if ( $^O eq 'MSWin32' ) {
		$self->{ENV} =  $ENV{CommonProgramFiles};
	}
	elsif ( $^O eq 'linux' || $^O eq 'darwin' ) {
		$self->{ENV} = '/tmp';
	}
	$self->{ENV} =~ s/\\/\//gsmx;
 	$self->{location} = $self->{ENV}.'/ADN/path_inc.xml';
	$self->path_inc();
 	$self->{XMLtoOBJ} = ADN::XML::XMLtoOBJ->new( $self->{location} );
	if ( $self->{XMLtoOBJ} =~ m/^[-]1$/sxm ) {
    		return -1;
 	}
 	@{$self->{XMLtoOBJ2}} = $self->{XMLtoOBJ}->conf_inc();
	if ( $self->_init() =~ m/^[-]1$/sxm ) {
    		return -1;
 	}
	return $self;
}

# #######################
# Fonction recuperation du path
# #######################
sub path_inc
{
	my $self = shift;

	foreach ( @INC ) {
    		my $path_inc = $_.'/ADN/';
    		if ( -f $path_inc.'conf.xml' ) {
      			my $xml = ADN::XML::OBJtoXML->new();
      			mkpath($self->{ENV}.'/ADN/'); 
      			if ( open FILE, '>', $self->{location} ) {
        			print FILE $xml->conf_inc($path_inc);
        			if (!close FILE) {}
      			}
    		}
  	}
  return;
}

# #######################
# Fonction lecture de fichier
# #######################
sub file_xml
{
	my $self = shift;

  	if (-e @{$self->{XMLtoOBJ2}}[0].'conf.xml' && -f @{$self->{XMLtoOBJ2}}[0].'conf.xml' && -r @{$self->{XMLtoOBJ2}}[0].'conf.xml') {
		if( open FILE, '<', @{$self->{XMLtoOBJ2}}[0].'conf.xml' ) {
			while ( my $l1 = <FILE>) {
				$self->{xml} .= $l1;
		  	}
			if (!close FILE) {}
	  	}
	}
  	return ($self->division_xml());
}

# #######################
# Fonction Mise en place XML
# #######################
sub division_xml
{
	my $self = shift;

 	if ($self->{xml} =~ m/\<$self->{balise}\>/smx) {
		$self->{xml} =~ s/(\<[^\>]+\>)[^\<&^\>]*(\<\/[^\>]+\>)/$1$2/gsmx;
		$self->{xml} =~ s/\s|\n|\r//gsmx;
  		$self->{xml} =~ s/^\<[^\>]+\>(.*)\<[^\>]+\>$/$1/smx;
    		$self->{xml} =~ s/^.*\<$self->{balise}\>(.*)\<\/$self->{balise}\>.*$/$1/smx;
		$self->{xml} =~ s/\<\/[^\>]+\>//gsmx;
  		$self->{xml} =~ s/^\<|\>$//gsmx;
  		@{$self->{xml1}} = split /\>\</smx, $self->{xml};
    		return @{$self->{xml1}};
  	}
  	else {
    		return '-1';
  	}
}

# #######################
# Fonction initialisation
# #######################
sub _init
{
	my $self = shift;

	if ( scalar(@{$self->{XMLtoOBJ2}}) > 0 ) {
  		$self->{obj_xml} = ADN::XML::XMLtoOBJ->new( @{$self->{XMLtoOBJ2}}[0] . 'conf.xml' );
    		if ( $self->{obj_xml} =~ m/^[-]1$/sxm ) {
      			return -1;
    		}
  	}
  	else {
    		return -1;
  	}
}

# #######################
# Fonction initialisation
# #######################
sub _send_path
{
  	my $self = shift;
  	return @{$self->{XMLtoOBJ2}}[0];
}

# #######################
# Fonction DESTROY
# #######################
sub DESTROY
{
  	my $self = shift;
  	undef $self;
  	return;
}

# #######################
# Fonction AUTOLOAD
# #######################
sub AUTOLOAD
{
	my $self = shift;
  	$self->{balise} = $AUTOLOAD;
  	my %hach;
  	my ($balise);
  	my (@tab, @balise);
  	$self->{balise} =~ s/.*:://smx;

	if ( ( $self->file_xml() ) =~ m/^[-]1$/sxm ) {
    		return -1;
  	}
  	else {
    		$balise = $self->{balise};
    		@balise = $self->{obj_xml}->$balise();
    		foreach (@{$self->{xml1}}) {
      			@tab = $balise[0]->$_();
      			$hach{$_} = $tab[0];
    		}
    		return %hach;
  	}
}
1;
__END__
